package hu.mapro.gwtui.gxt.client.ui;

import hu.mapro.gwtui.gxt.client.fieldseditor.FieldsEditorBuilder;
import hu.mapro.gwtui.gxt.client.fieldseditor.Interface;
import hu.mapro.gwtui.gxt.client.fieldseditor.field.EditorFieldInterface;
import hu.mapro.model.Getter;
import hu.mapro.model.Setter;
import hu.mapro.model.meta.Field;
import hu.mapro.model.meta.HasLabel;

import com.google.common.base.Function;
import com.google.web.bindery.requestfactory.shared.BaseProxy;

public abstract class TypeFieldsEditor<
	T extends BaseProxy, 
	RW extends Field<T,?>&HasLabel&Getter<T, ?>&Setter<T, ?>
> extends hu.mapro.gwtui.gxt.client.recordseditor.requestfactory.TypeEditorAdapter<T> {
	
	protected RW[] editorFields;
	protected Function<? super RW, EditorFieldInterface<?>> createFieldFunction;
	
	protected Interface<T, RW> buildFieldsEditorMainInterface() {
		return new hu.mapro.gwtui.gxt.client.fieldseditor.field.Adapter<T, RW>(new hu.mapro.gwtui.gxt.client.fieldseditor.field.function.Adapter<RW>(createFieldFunction));
	}

	public void setCreateFieldFunction(
			Function<? super RW, EditorFieldInterface<?>> createFieldFunction) {
		this.createFieldFunction = createFieldFunction;
	}

	public void setEditorFields(RW[] editorFields) {
		this.editorFields = editorFields;
	}
	
	@Override
	protected void initialize() {
		FieldsEditorBuilder<T, RW> fieldsEditorBuilder = new FieldsEditorBuilder<T, RW>();
		fieldsEditorBuilder.setMainInterface(new hu.mapro.gwtui.gxt.client.fieldseditor.field.Adapter<T, RW>(new hu.mapro.gwtui.gxt.client.fieldseditor.field.function.Adapter<RW>(createFieldFunction)));
		fieldsEditorBuilder.add(editorFields);
		setFieldsEditorComponentProvider(fieldsEditorBuilder );
		
		super.initialize();
	}

}
